# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
from ui_referenceEdit import Ui_ReferenceEditFrame
from MySQLdb import OperationalError

class ReferenceEdit(QtGui.QFrame):
	def __init__(self, controller, referenceClass, mode='New', readonly=False):
		QtGui.QFrame.__init__(self)

		self.controller = controller

		self.referenceClass = referenceClass
		self.reference = None

        # Set up the user interface from Designer.
		self.ui = Ui_ReferenceEditFrame()
		self.ui.setupUi(self)

		self.setMode(mode)

		self.readonly = readonly
		self.setReadonly(self.readonly)

		self.connect(self.ui.addPushButton, QtCore.SIGNAL("clicked()"), self.insertReference)
		self.connect(self.ui.updatePushButton, QtCore.SIGNAL("clicked()"), self.updateReference)
		self.connect(self.ui.searchPushButton, QtCore.SIGNAL("clicked()"), self.search)


	def insertReference(self):
		self.reference = self.referenceClass()

		self.reference.insert()

		self.updateReference()

		if self.mode.find("Dialog") != -1:
			self.controller.addToReaction(self.reference, "Reference")


	def updateReference(self):

		self.reference.name = self.ui.nameLineEdit.text()
		self.reference.year = self.ui.yearSpinBox.value()
		self.reference.author_name = self.ui.authorLineEdit.text()
		self.reference.source = self.ui.journalLineEdit.text()
		self.reference.volume = self.ui.volumeSpinBox.value()
		self.reference.pages = self.ui.pagesLineEdit.text()


	def search(self):
		self.reference = self.referenceClass()
		self.reference.name = self.ui.nameLineEdit.text()
		self.reference.year = self.ui.yearSpinBox.value()
		self.reference.author_name = self.ui.authorLineEdit.text()
		self.reference.source = self.ui.journalLineEdit.text()
		self.ui.volumeSpinBox.value()
		self.ui.pagesLineEdit.text()

		if self.mode.find("Dialog") == -1:
			self.controller.searchReference(self.reference)
		else:
			self.controller.searchReferenceFromDialog(self.reference)


	def clear(self):
		self.ui.nameLineEdit.clear()
		self.ui.yearSpinBox.setValue(2000)
		self.ui.authorLineEdit.clear()
		self.ui.journalLineEdit.clear()
		self.ui.volumeSpinBox.setValue(1)
		self.ui.pagesLineEdit.clear()

	def setCurrentReference(self, reference, readonly = False):
		self.reference = reference
		self.updateFields()

		self.setReadonly(readonly)

	def setReadonly(self, readonly):
		self.readonly = readonly

		self.ui.nameLineEdit.setReadOnly(readonly)
		self.ui.yearSpinBox.setReadOnly(readonly)
		self.ui.authorLineEdit.setReadOnly(readonly)
		self.ui.journalLineEdit.setReadOnly(readonly)
		self.ui.volumeSpinBox.setReadOnly(readonly)
		self.ui.pagesLineEdit.setReadOnly(readonly)

	def setMode(self, mode):
		self.mode = mode
		if self.mode.find("New") != -1:
			self.ui.addPushButton.show()
			self.ui.updatePushButton.hide()
			self.ui.searchPushButton.hide()
		elif mode == "Search":
			self.ui.addPushButton.hide()
			self.ui.updatePushButton.show()
			self.ui.searchPushButton.show()
		elif mode == "Search Dialog":
			self.ui.addPushButton.hide()
			self.ui.updatePushButton.hide()
			self.ui.searchPushButton.show()

	def updateFields(self):
		self.ui.nameLineEdit.setText(self.reference.name)
		self.ui.yearSpinBox.setValue(int(self.reference.year))
		self.ui.authorLineEdit.setText(self.reference.author_name)
		self.ui.journalLineEdit.setText(self.reference.source)
		self.ui.volumeSpinBox.setValue(int(self.reference.volume))
		self.ui.pagesLineEdit.setText(self.reference.pages)